package com.ltc.smstimer.timer;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.ltc.smstimer.R;

public class TimerSendActivity extends Activity{
	
	String address;
	String messageText;
	
	TextView addressTV;
	TextView messageTV;
	TextView dateTV;
	TextView timeTV;
	
	Button setDateBt;
	Button setTimeBt;
	Button okBt;
	Button quitBt;
	
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;
	
	Calendar calendar=Calendar.getInstance();
	Intent mainIntent;
	Bundle mainBundle;
	AlarmManager am;

	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//隐藏Activity默认Title
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    //设置Activity对用的xml布局文件
	    setContentView(R.layout.timer_send_message);
	    
	    
	    Bundle bundle = this.getIntent().getExtras();
	    address = bundle.getString("address");
	    messageText = bundle.getString("messageText");
	    
	    
	    //绑定控件
		addressTV = (TextView)findViewById(R.id.timer_address);
		messageTV = (TextView)findViewById(R.id.timer_message);
		dateTV = (TextView)findViewById(R.id.timer_date);
		timeTV = (TextView)findViewById(R.id.timer_time);
		
		addressTV.setText("联系人：\n"+address);
		messageTV.setText("短信内容：\n"+messageText+"\n\n");
		
		//
		setDateBt = (Button)findViewById(R.id.timer_setdate);
		setDateBt.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mYear=calendar.get(Calendar.YEAR);
		    	mMonth=calendar.get(Calendar.MONTH);
		    	mDay=calendar.get(Calendar.DAY_OF_MONTH);
		    	
				
				// TODO Auto-generated method stub
				new DatePickerDialog(TimerSendActivity.this,new DatePickerDialog.OnDateSetListener() 
				{
					
					
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						// TODO Auto-generated method stub
						
						mYear=year;
		        		mMonth=monthOfYear;
		        		mDay=dayOfMonth;
		        		updateDateDisplay();
		        		Toast.makeText(TimerSendActivity.this,"set date ok",Toast.LENGTH_LONG).show(); 
					}

					private void updateDateDisplay() {
						// TODO Auto-generated method stub
						dateTV.setText("日期："+new StringBuilder().append(mYear).append("/")
								.append(format(mMonth + 1)).append("/")
								.append(format(mDay))
								);
					}

					
				},mYear,mMonth,mDay).show();
			}
		});
		//
		setTimeBt = (Button)findViewById(R.id.timer_settime);
		setTimeBt.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mHour=calendar.get(Calendar.HOUR_OF_DAY);
		    	mMinute=calendar.get(Calendar.MINUTE);
				new TimePickerDialog(TimerSendActivity.this,new TimePickerDialog.OnTimeSetListener() {
					
					
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						// TODO Auto-generated method stub

						mHour=hourOfDay;
		        		mMinute=minute;
		        		updateTimeDisplay();
		        		Toast.makeText(TimerSendActivity.this,"set time ok",Toast.LENGTH_LONG).show(); 
					}

					private void updateTimeDisplay() {
						// TODO Auto-generated method stub
						timeTV.setText("时间："+new StringBuilder().append(format(mHour)).append(":")
								.append(mMinute)
								);
					}
				},mHour,mMinute,true).show();
			}
		});
		//
		okBt = (Button)findViewById(R.id.timer_OK);
		okBt.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				calendar.set(Calendar.YEAR,mYear);
				calendar.set(Calendar.MONTH,mMonth);
				calendar.set(Calendar.DAY_OF_MONTH,mDay);
				calendar.set(Calendar.HOUR_OF_DAY,mHour);
				calendar.set(Calendar.MINUTE,mMinute);

				try
    			{
    				//将信息进行存储
    				SharedPreferences sp = getSharedPreferences("message",0);
    				SharedPreferences.Editor editor = sp.edit(); 

    				editor.putString("phoneNumber",address); 
    				editor.putString("messageText",messageText);
    				editor.commit(); 

    				//设立闹钟服务
    				Intent intent = new Intent(TimerSendActivity.this,SmsAlarmReceiver.class);
    				PendingIntent sender =PendingIntent.getBroadcast(TimerSendActivity.this, 0,intent, 0);
    				am = (AlarmManager)getSystemService(ALARM_SERVICE);
    				am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
    				Toast.makeText(TimerSendActivity.this,"save success",Toast.LENGTH_LONG).show(); 
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
			}
		});
		//
		quitBt = (Button)findViewById(R.id.timer_QUIT);
		quitBt.setOnClickListener(new OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
	/**
	 * 
	 */
	private Object format(int i) {
		// TODO Auto-generated method stub
		String s=""+i;
		if(s.length()==1)
			s="0"+s;
		return s;
	}
	
}
